$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'lanling/question/list',
        datatype: "json",
        colModel: [
            {label: 'id', name: 'id', index: 'id', width: 50, key: true, hidden: true},
            {label: '题库名称', name: 'subjectName', index: 'subjectName', width: 100, align: "center"},
            // {label: '题库编号', name: 'serialNumber', index: 'serialNumber', width: 80, align: "center"},
            {label: '题目内容', name: 'questionContent', index: 'question_content', width: 200, align: "center"},
            {label: '选项内容', name: 'optionContent', index: 'option_content', width: 80, align: "center"},
            {label: '正确答案', name: 'trueAnswer', index: 'true_answer', width: 80, align: "center"},
            {
                label: '题目类型',
                name: 'questionType',
                index: 'question_type',
                width: 80,
                align: "center",
                formatter: function (value) {
                    if (value === 1) {
                        return '<span class="label label-success">普通题目</span>'
                    } else if (value === 2) {
                        return '<span class="label label-success">图片题目</span>'
                    } else if (value === 3) {
                        return '<span class="label label-success">视频题目</span>'
                    } else {
                        return '<span class="label label-danger">未知</span>'
                    }
                }
            },
            {
                label: '备注', name: 'remark', index: 'remark', width: 80, align: "center",
                formatter: function (value) {
                    return (value === '' || value == null) ? "无" : value;
                }
            }
            // {label: '创建时间', name: 'createDate', index: 'create_date', width: 80, align: "center"},
        ],
        viewrecords: true,
        height: 385,
        rowNum: 10,
        rowList: [10, 30, 50],
        rownumbers: true,
        rownumWidth: 25,
        autowidth: true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader: {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames: {
            page: "page",
            rows: "limit",
            order: "order"
        },
        gridComplete: function () {
            //隐藏grid底部滚动条
            $("#jqGrid").closest(".ui-jqgrid-bdiv").css({"overflow-x": "hidden"});
        }
    });

});

var vm = new Vue({
    el: '#rrapp',
    data: {
        showList: true,
        title: null,
        question: {
            imgUrl: ''
        },
        ruleValidate: {
            name: [
                {required: true, message: 'name不能为空', trigger: 'blur'},
                {max: 100, message: '请输入最多100位'}
            ]
        },
        q: {
            name: '',
            subjectId: ''
        },
        showImgUrl: false,
        showVideoUrl: false,
        subjectList: [],
        subjectListByQues: []
    },
    mounted() {
        this.queryListByQues();
    },
    methods: {
        query: function () {
            vm.reload();
        },
        add: function () {
            vm.showList = false;
            vm.title = "新增";
            vm.question = {
                optionType: '1',
                questionType: '1',
                imgUrl: ''
            };
            vm.subjectList = [];
            vm.getSignUpSubjectList();
        },
        update: function (event) {
            var id = getSelectedRow();
            if (id == null) {
                return;
            }
            vm.showList = false;
            vm.title = "修改";
            vm.getSignUpSubjectList();
            vm.getInfo(id)
        },
        saveOrUpdate: function (event) {
            var url = vm.question.id == null ? "lanling/question/save" : "lanling/question/update";
            $.ajax({
                type: "POST",
                url: baseURL + url,
                contentType: "application/json;charset=utf-8",
                data: JSON.stringify(vm.question),
                success: function (r) {
                    if (r.code === 0) {
                        alert('操作成功', function (index) {
                            vm.reload();
                        });
                    } else {
                        alert(r.msg);
                    }
                }
            });
        },
        del: function (event) {
            var ids = getSelectedRows();
            if (ids == null) {
                return;
            }

            confirm('确定要删除选中的记录？', function () {
                $.ajax({
                    type: "POST",
                    url: baseURL + "lanling/question/delete",
                    contentType: "application/json;charset=utf-8",
                    data: JSON.stringify(ids),
                    success: function (r) {
                        if (r.code == 0) {
                            alert('操作成功', function (index) {
                                $("#jqGrid").trigger("reloadGrid");
                            });
                        } else {
                            alert(r.msg);
                        }
                    }
                });
            });
        },
        getInfo: function (id) {
            $.get(baseURL + "lanling/question/info/" + id, function (r) {
                vm.question = r.question;
                vm.question.questionType = vm.question.questionType + '';
                vm.question.optionType = vm.question.optionType + '';
            });
        },
        reload: function (event) {
            vm.showList = true;
            var page = $("#jqGrid").jqGrid('getGridParam', 'page');
            $("#jqGrid").jqGrid('setGridParam', {
                postData: {'name': vm.q.name, 'subjectId': vm.q.subjectId},
                page: page
            }).trigger("reloadGrid");
            vm.handleReset('formValidate');
        },
        handleSubmit: function (name) {
            handleSubmitValidate(this, name, function () {
                vm.saveOrUpdate()
            });
        },
        handleReset: function (name) {
            handleResetForm(this, name);
        },
        questionTypeChange: function () {
            var questionType = vm.question.questionType;
            if (questionType == 1) {
                vm.showImgUrl = false;
                vm.showVideoUrl = false;
            } else if (questionType == 2) {
                vm.showImgUrl = true;
                vm.showVideoUrl = false;
            } else if (questionType == 3) {
                vm.showVideoUrl = true;
                vm.showImgUrl = false;
            }
        },
        handleSuccessPicUrl: function (res, file) {
            vm.question.imgUrl = file.response.url;
        },
        handleFormatError: function (file) {
            this.$Notice.warning({
                title: '文件格式不正确',
                desc: '文件 ' + file.name + ' 格式不正确，请上传 jpg 或 png 格式的图片。'
            });
        },
        handleMaxSize: function (file) {
            this.$Notice.warning({
                title: '超出文件大小限制',
                desc: '文件 ' + file.name + ' 太大，不能超过 2M。'
            });
        },
        eyeImagePicUrl: function () {
            var url = vm.school.schoolLogo;
            eyeImage(url);
        },
        handleSuccessVideoUrl: function (res, file) {
            vm.question.videoUrl = file.response.url;
        },
        handleVideoFormatError: function (file) {
            this.$Notice.warning({
                title: '文件格式不正确',
                desc: '文件 ' + file.name + ' 格式不正确，请上传 mp4 格式的视频。'
            });
        },
        handleVideoMaxSize: function (file) {
            this.$Notice.warning({
                title: '超出文件大小限制',
                desc: '文件 ' + file.name + ' 太大，不能超过 20M。'
            });
        },
        getSignUpSubjectList: function () {
            $.get("../../lanling/subject/queryList", function (r) {
                vm.subjectList = r.list;
            });
        },
        queryListByQues: function () {
            $.get("../../lanling/subject/queryListByQues", function (r) {
                vm.subjectListByQues = r.list;
            });
        }
    }
});